package com.example.yichuangyun.mvvm.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;

import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.ActivitySearchAddressBookBinding;
import com.example.yichuangyun.dialog.MyDialog;
import com.example.yichuangyun.dialog.SinglePickerView;
import com.example.yichuangyun.model.contacts.AddressDataModel;
import com.example.yichuangyun.mvvm.base.BaseActivity;
import com.example.yichuangyun.mvvm.ui.activity.view.SearchAddressBookViewModel;
import com.example.yichuangyun.mvvm.ui.adapter.AddressBookAdapter;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.RequestUtils;
import com.example.yichuangyun.utils.ToastHelper;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索通讯录
 */
public class SearchAddressBookActivity extends BaseActivity<ActivitySearchAddressBookBinding, SearchAddressBookViewModel> {

    private int contactsType = 1;
    private int page = 1;
    private List<AddressDataModel.AddressModel> data = new ArrayList<>();
    private AddressBookAdapter addressBookAdapter;
    private String contactId;
    private String clientName;
    private String mTelNumber;
    private MyDialog myDialog;

    @Override
    protected SearchAddressBookViewModel bindModel() {
        return getViewModel(this, SearchAddressBookViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        initDialog();
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            contactsType = bundle.getInt(Constant.typeKey, 1);
        }
        binding.setMSearchAddressBookViewModel(viewModel);
        addressBookAdapter = new AddressBookAdapter(context, data, contactsType);
        binding.recyclerView.setAdapter(addressBookAdapter);
    }

    @Override
    protected void initClick() {
        viewModel.contactsData.observe(this, addressDataModel -> {
            List<AddressDataModel.AddressModel> list = addressDataModel.getRecords();
            if (addressDataModel.getTotal() > 0) {
                binding.llCount.setVisibility(View.VISIBLE);
                binding.tvCount.setText(String.valueOf(addressDataModel.getTotal()));
                binding.tvText.setText(getString(R.string.search_my_contacts_count));
            } else {
                binding.llCount.setVisibility(View.GONE);
            }
            if (list.size() < Constant.pageSize) {
                binding.smartRefreshLayout.finishLoadMoreWithNoMoreData();
            }
            if (page == 1) {
                data = list;
                addressBookAdapter.onRefresh(list);
                binding.smartRefreshLayout.finishRefresh();
            } else {
                addressBookAdapter.addData(list);
                binding.smartRefreshLayout.finishLoadMore();
            }
        });

        binding.tvCancel.setOnClickListener(v -> finish());
        binding.etSearchContent.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                String content = viewModel.keyWords.getValue();
                if (content != null && content.trim().length() > 0) {
                    CommonUtil.closeKeyboard(SearchAddressBookActivity.this);
                    page = 1;
                    binding.recyclerView.scrollToPosition(0);
                    binding.smartRefreshLayout.autoRefresh();
                } else {
                    binding.llCount.setVisibility(View.GONE);
                    data.clear();
                    addressBookAdapter.onRefresh(data);
                    ToastHelper.show(getString(R.string.please_address_search_keywords));
                }
                return true;
            }
            return false;
        });

        addressBookAdapter.setOnCallListener((telList, id, userName) -> {
            contactId = id;
            clientName = userName;
            if (telList.size() > 1) {
                initSinglePickerView(telList);
            } else if (telList.size() == 1) {
                mTelNumber = telList.get(0);
                RequestUtils.checkCallNum(context, mTelNumber, myDialog, 0, true);
            }
        });

        binding.smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (!TextUtils.isEmpty(binding.etSearchContent.getText())) {
                    page++;
                    viewModel.getContactsList(page, viewModel.keyWords.getValue(), contactsType);
                } else {
                    binding.smartRefreshLayout.finishLoadMoreWithNoMoreData();
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                if (!TextUtils.isEmpty(binding.etSearchContent.getText())) {
                    page = 1;
                    viewModel.getContactsList(page, viewModel.keyWords.getValue(), contactsType);
                } else {
                    binding.smartRefreshLayout.finishRefresh();
                }
            }
        });
    }

    private void initSinglePickerView(List<String> data) {
        SinglePickerView singlePickerView = new SinglePickerView(this, getString(R.string.call_num), data) {
            @Override
            public void confirm(String label, int position) {
                mTelNumber = label;
                RequestUtils.checkCallNum(context, mTelNumber, myDialog, 0, true);
            }
        };
        singlePickerView.showSinglePickerView();
    }

    private void initDialog() {
        myDialog = new MyDialog(this) {
            @Override
            public void leftClick() {
                RequestUtils.callNum(context, mTelNumber);
                RequestUtils.addContactsFollow(contactId, clientName, mTelNumber);
                Constant.pushBusinessId = "1";
            }

            @Override
            public void rightClick() {
                RequestUtils.callbackOutbound2(context, mTelNumber, contactId, clientName);
            }
        };
    }
}